package com.xiaoming;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;

public class LinClassLoader extends ClassLoader {
    private String path;

    public LinClassLoader(String path) {
        this.path = path;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        try {
            String classPath = path + "\\" + name.replaceAll("\\.", "\\\\") + ".class";
            FileInputStream in = new FileInputStream(classPath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len = -1;
            while ((len = in.read(buf)) != -1) {
                baos.write(buf, 0, len);
            }
            in.close();
            byte[] classBytes = baos.toByteArray();
            return defineClass(name, classBytes, 0, classBytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return super.findClass(name);
    }
}
